import Taro from '@tarojs/taro';
import ENV_TYPE = Taro.ENV_TYPE;
import {H5FileSystem} from "./H5FileSystem";
import {WeappFileSystem} from "./WeappFileSystem";
import ImageFile = Taro.chooseImage.ImageFile;

export interface ChooseImageResult{
  imageFile: ImageFile,
  base64: string
}

export interface FileSystem{
  /**
   * 选择图片并返回图片的base64
   * @param option
   */
  chooseImage: (option: Taro.chooseImage.Option) => Promise<ChooseImageResult[]>
}

let fileSystem: FileSystem | null = null


export function getFileSystem(): FileSystem{
  if(!fileSystem){
    fileSystem = newFileSystem()
  }
  return fileSystem
}

function newFileSystem(): FileSystem{
  if (Taro.getEnv() === ENV_TYPE.WEB) {
    return new H5FileSystem()
  }else {
    return new WeappFileSystem()
  }
  throw new Error("还未实现当前环境的FileSystem")
}


export * from './chooseFile'
